#include <iostream>
#include "stack.h"
#include <string>
#include <cmath>
using namespace std;


// parse int , reture index ,get result
int parseInt(string &s,Stack<int> &nums,int index){
    // cout<<"=> parseint entered"<<endl;
    int count = 0;
    char current ;
    int negetive = 0;
    while(1){
        current = s[index];
        if (current == '-'){
            //check - as negetive
            negetive = 1;
            index++;
            continue;
        }
        if(current >= '0' and current <= '9'){
            count = count*10 + (current-48);
            index++;
            continue;
        }else{
            break;
        }
    }
    if(negetive) count=-count;

    cout<<"=> push num : "<<count<<endl;
    nums.push(count);
    return index;
}
void cal(Stack<int> &nums,char c){
    int b = nums.pop();
    int a = nums.pop();
    int res = 0;
    switch (c)
    {
    case '+':
        res = a+b;
        break;
    case '-':
        res = a-b;
        break;
    case '*':
        res = a*b;
        break;
    case '/':
        res = a/b;
        break;
    default:
        cout<<"=> call error: "<<a<<b<<endl;
        return;
    }
    nums.push(res);
}

int getIndex(char c){
    // 0
    char ops[3][2] = {
        {'(',')'},
        {'+','-'},
        {'*','/'}
    };
    for(int i = 0;i<3;i++){
        for(int j = 0;j<2;j++){
            if(c  == ops[i][j]) return i;
        }
    }
    return -1;
}
// parse int , reture index ,get result
int parseOp(string &s,Stack<char> &ops,Stack<int> &nums,int index){
    // cout<<"=> parseop enterd"<<index<<s.length()<<endl;
    char current ;
    int negetive = 0;
    if (index == s.length()) {
        cal(nums,ops.pop());
        cout<<"== the result is "<<nums.pop()<<endl;
        exit(1);
    }

    while(1){
        current = s[index];
        
        if (getIndex(current) == -1) break;
        

        if(ops.length() ==0) {
            ops.push(current);
            cout<<"=> push op : "<<current<<endl;
            index++;
            break;
        }
        // if precent gt top cal
        if(getIndex(ops.top())<getIndex(current)){
            cal(nums,current);
            index++;
        }
        if(getIndex(ops.top())==getIndex(current)){
            cout<<"error: same op level"<<endl;
            break;
        }
        if(getIndex(ops.top())>getIndex(current)){
            ops.push(current);
            index++;
        }

        
    }
    return index;
}


void parseExp(string &s,Stack<char> &ops,Stack<int> &nums){
    int index = 0;
    while (index<s.length()){
        cout<<"=> loop index : "<<index<<endl;
        index = parseInt(s,nums,index);
        index = parseOp(s,ops,nums,index);
    }
    


}


int main(){
    cout<<"please input the expression: ";
    string input;
    cin>>input;
    Stack<char> ops;
    Stack<int> nums;
    parseExp(input,ops,nums);


}